package com.sjc.html2md;

import com.alibaba.fastjson.JSONObject;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.*;
import java.net.URLEncoder;

public class Html2Markdown {
    public static void main(String[] args) throws IOException {
        String url = "https://gate.lagou.com/v1/neirong/kaiwu/getCourseLessonDetail?lessonId="+ "6769";
        String htmlJson = getHtmlJson(url);
        JSONObject jsonObject = JSONObject.parseObject(htmlJson);
        String html = jsonObject.getJSONObject("content").getString("textContent");
        String markdown = HTML2Md.convertHtml(html, "utf-8");
        System.out.println(handleMd(markdown));
    }

    private static String handleMd(String markdown){
        markdown = markdown.replaceAll("\\\\n", "\n");
        markdown = markdown.replaceAll("\\\\t", "\t");
        markdown = markdown.replaceAll("\\* \\n\\n\\n", "- ");

        markdown = markdown + "\n ";
        String[] mkArr = markdown.split("\n");
        StringBuilder builder = new StringBuilder();
        builder.append(mkArr[0]).append("\n");
        for(int i=0;i<mkArr.length-1;i++){
            if(i>1 && !mkArr[i].equals(mkArr[i-1])){
                builder.append(mkArr[i]).append("\n");
            }
        }
        markdown = builder.toString();
        markdown = markdown.replaceAll("\\n- ", "- ");
        markdown = "[toc] \n\n" + markdown.replaceAll("1\\. \\n", "1. ");
        markdown = markdown.replaceAll("### \\n","### ");
        return markdown;
    }

    public static String getHtmlJson(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String cookie = "user_trace_token=20210708102008-59697f2e-0b30-454c-b082-85b9c16a5d39; _ga=GA1.2.1888410897.1625710809; LGUID=20210708102009-d2c9e7be-73a2-4daf-b6a9-aa34194ab86b; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1625710809,1625710813; gate_login_token=842a7033b3c8589a7cbe3a62eb79877c8de4035e6484a7af; privacyPolicyPopup=false; index_location_city=广州; smidV2=20210708102135a39ff599e63d2b9633f9dbf9f63a10d6001866ad814392620; thirdDeviceIdInfo=[{\"channel\":1,\"thirdDeviceId\":\"WHJMrwNw1k/HoxZ8BZ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czUcjiQC32Lco44BMYXrQ+AIOfEccJKHpj0vJ+ngq/27aqj1hWq8tEPFFjdnxMSfKgAnjbIEAX3F9CIW8BSiMHYmPBt7FDDY0CCVFICHr2dp5gQVGvhfbqg7VzvNsw==\"; sensorsdata2015session={}; X_HTTP_TOKEN=321cc40993fb8c6c5186975261ba2675afe00fea53; _putrc=280EAD60210B4867; login=true; unick=桑甲存; sensorsdata2015jssdkcross={\"distinct_id\":\"3796025\",\"first_id\":\"17a83ec76a0c1-03ca4ec7fe8431-f7f1939-4953600-17a83ec76a1428\",\"props\":{\"$latest_traffic_source_type\":\"直接流量\",\"$latest_search_keyword\":\"未取到值_直接打开\",\"$latest_referrer\":\"\",\"$os\":\"Windows\",\"$browser\":\"Chrome\",\"$browser_version\":\"91.0.4472.77\"},\"$device_id\":\"17858361e6a2ac-0825699478c202-5771031-4953600-17858361e6b2a3\"}";
        String xlreqHeader = "{\"deviceType\":1,\"userToken\":\"$$$EDU_eyJraWQiOiJjYzQzMjJkNi00OTUxLTQ5ZDEtYjljYS03NTFmM2Q1NGNlYzYiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ0Z3QiOiJfQ0FTX1RHVF9UR1QtODdkNGZjNzM2YzRkNDRiNGJhMzY4M2Q2NmE1NTA0OGEtMjAyMTA3MTUxNDQ0NTctX0NBU19UR1RfIiwic3ViIjoiMzc5NjAyNSIsImlwIjoiMTE0LjI1MS4xNzIuMzQiLCJpc3MiOiJlZHUubGFnb3UuY29tIiwidG9rZW5UeXBlIjoxLCJleHAiOjE2MjY5MzYyOTcsImlhdCI6MTYyNjMzMTQ5N30.Yz1niHUlfHpuBtFRL2ZC-UP_dt-ifl1Y3t36uhqktMYsG8dwpmf3Up5m733W5L1xvFR4WgGBC25xOLUyDhZuDxHFxtuAeUWo1nr4q6vf3YP09oAjeSc1OBcHxLPlRCZ5U8HtjviAK2wAUzGBeeZuu3anAPe14FtW5MZCWX_YUs7dczgHZsnscuT1YX-DEj22ZL6O3W954pJscdeEsmzMSZ3K4Ul8CT-DPBeKS_qvwUBjkCodGtQkXJl73cGFjjflM60Bo7Zp0aR4x47fZ4sY3wtRnpqKGZI_1aGAhqt41tjkHjHrFBmPdUfhiX7xMQbkFGoQV-zg8xKL0bZjWH8n6g\"}";
        String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36";

        // 首先构造一个Request对象，参数最起码有个url，当然可以通过Request.Builder设置更多的参数比如：header、method等
        Request request = new Request.Builder().get()
                .url(url)
                .header("Cookie", getValueEncoded(cookie))
                .header("user-agent", getValueEncoded(userAgent))
                .header("x-l-req-header", getValueEncoded(xlreqHeader))
                .build();

        // 通过request的对象去构造得到一个Call对象，类似于将请求封装成了任务
        Call call = client.newCall(request);

        // 同步调用,返回Response，需要这里会抛出IO异常
        Response response = call.execute();
        if (response.isSuccessful()) {
            return  response.body().string();
        }
        return "";
    }

    //由于okhttp header 中的 value 不支持 null, \n 和 中文这样的特殊字符,所以这里
    //会首先替换 \n ,然后使用 okhttp 的校验方式,校验不通过的话,就返回 encode 后的字符串
    private static String getValueEncoded(String value) throws UnsupportedEncodingException {
        if (value == null) {
            return "null";
        }
        String newValue = value.replace("\n", "");
        for (int i = 0, length = newValue.length(); i < length; i++) {
            char c = newValue.charAt(i);
            if (c <= '\u001f' || c >= '\u007f') {
                return URLEncoder.encode(newValue, "UTF-8");
            }
        }
        return newValue;
    }
}
